package com.erp.erp_ui.OpeationLog;

import com.erp.erp_entitys.OperationLog.OperationLogEntity;
import com.erp.erp_entitys.OperationLog.OperationLogVoEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.OperationLog.IOperationLogService;
import com.erp.erp_servers.User.IUserService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OperationLogControl
 * @Description 组织机构操作日志控制类
 * @Author songjizhen
 * @Date 2020-06-30 21:41
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "OperationLog")
 public class OperationLogControl extends Base {
    @Autowired
    IOperationLogService operationLogService;
    @Autowired
    IUserService userService;
    /**
     * 新增组织机构操作日志
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody OperationLogEntity record) {
        try {
            UserEntity user=(UserEntity)this.getBean("user");
            record.setOperator(user.getCode());
            record.setUpdateTime(new Date());
            int num = operationLogService.insertSelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 查询组织机构操作日志
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody

    public R<List<OperationLogVoEntity>> selectByPage(String companyCode){
        try {
            OperationLogEntity record=new OperationLogEntity();
            record.setCompanyCode(companyCode);
           List<OperationLogEntity> operationLogEntities = operationLogService.selectByExample(record);
            List<OperationLogVoEntity> list=new ArrayList<OperationLogVoEntity>();
            list=this.converVo(operationLogEntities);
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
  private List<OperationLogVoEntity> converVo( List<OperationLogEntity> pos){
      List<OperationLogVoEntity> vos=new ArrayList<OperationLogVoEntity>();
      for(int i=0;i<pos.size();i++){
          OperationLogEntity ole = pos.get(i);
          OperationLogVoEntity vo=new OperationLogVoEntity();
          vo.setCompanyCode(ole.getCompanyCode());
          vo.setDesc(ole.getDesc());
          vo.setFlag(ole.getFlag());
          UserEntity user = userService.selectUserByCode(ole.getOperator());
          vo.setOperator(user.getName());
          vo.setType(ole.getType());
          vo.setUpdateTime(DateUtils.format(ole.getUpdateTime(),DateUtils.DATE_TIME_PATTERN));
          vos.add(vo);
      }
      return vos;
  }
    /**
     * 根据所属公司查询操作日志
     * @param CompanyCode
     * @return
     */
    @RequestMapping(value = "selectByCompanyCode")
    @ResponseBody
    public R<List<OperationLogEntity>> selectByCompanyCode(String CompanyCode){
        try {
            OperationLogEntity record=new OperationLogEntity();
            record.setCompanyCode(CompanyCode);
            List<OperationLogEntity> operationLogEntities = operationLogService.selectByExample(record);

            return R.build(operationLogEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
